﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Net;
using System.Net.Sockets;

namespace eLibCommon
{
  public static class TcpUtils
  {
    private static object _lock = new object();
    public static void SendText(TcpClient client, string message)
    {
      lock (_lock)
      {
        NetworkStream clientStream = client.GetStream();        
        byte[] buffer = Encoding.ASCII.GetBytes(message);
        clientStream.Write(buffer, 0, buffer.Length);
        clientStream.Close();
        //client.Client.Close();
        //client.Close();
      }
    }

    public static string ReceiveText(TcpClient client)
    {
      lock (_lock)
      {
        StringBuilder sb = new StringBuilder();
        int i;
        NetworkStream clientStream = client.GetStream();
        byte[] buffer = new byte[256];
        while ((i = clientStream.Read(buffer, 0, buffer.Length)) != 0)
          sb.Append(Encoding.ASCII.GetString(buffer, 0, i));
        //clientStream.Close();
        //client.Client.Close();
        //client.Close();
        return sb.ToString();      
      }
    }


  }
}
